package com.bw.doctor.service.impl;/**
 * @Author：尚志豪
 * @Package：com.bw.doctor.service.impl
 * @Project：doudi-group2-shixun-02
 * @name：PharmacyServiceImpl
 * @Date：2025/2/25 11:48
 */

import com.bw.common.core.domain.Result;
import com.bw.doctor.domain.DoctorMiddleRestoratives;
import com.bw.doctor.domain.Prescription;
import com.bw.doctor.domain.PrescriptionMiddleRestoratives;
import com.bw.doctor.domain.Restoratives;
import com.bw.doctor.domain.res.PrescriptionRes;
import com.bw.doctor.mapper.PharmacyMapper;
import com.bw.doctor.service.PharmacyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: doudi-group2-shixun-02
 * @description: 我的药房业务逻辑
 * @author: CuiFu Bo
 * @create: 2025-02-25 11:48
 **/
@Service
public class PharmacyServiceImpl implements PharmacyService {
    @Autowired
    private PharmacyMapper pharmacyMapper;
    /**
     * B端 我的药房 常用药列表
     */
    @Override
    public List<Restoratives> listOfCommonDrugs(Long doctorId) {

        // 通过医生id查询中间表
        List<DoctorMiddleRestoratives> middleRestorativesList = pharmacyMapper.queryTheIntermediateTableByDoctorId(doctorId);
        List<Long> restorativesIds = new ArrayList<>();
        for (DoctorMiddleRestoratives doctorMiddleRestoratives : middleRestorativesList) {
            restorativesIds.add(doctorMiddleRestoratives.getRestorativesId());
        }
        System.out.println("restorativesIds: " + restorativesIds);

        return pharmacyMapper.listOfCommonDrugs(restorativesIds);
    }
    /**
     * B端 我的药房 常用药删除
     */
    @Override
    public void deletionOfCommonDrugs(Long restorativesId, Long doctorId) {
        pharmacyMapper.deletionOfCommonDrugs(restorativesId,doctorId);
    }
    /**
     * B端 我的药房 常用药详情
     */
    @Override
    public Restoratives detailsOfCommonDrugs(Long restorativesId) {
        return pharmacyMapper.detailsOfCommonDrugs(restorativesId);
    }
    /**
     * B端 我的药房 常用药添加
     */
    @Override
    public void additionOfCommonDrugs(Long restorativesId, Long doctorId) {
        pharmacyMapper.additionOfCommonDrugs(restorativesId, doctorId);
    }
    /**
     *  B端 我的药房 常用处方列表
     */
    @Override
    public List<Prescription> listOfCommonPrescriptions(Long doctorId) {
        return pharmacyMapper.listOfCommonPrescriptions(doctorId);
    }
    /**
     * B端 我的药房 常用处方添加
     */
    @Override
    @Transactional
    public void commonlyPrescribedAddition(Prescription prescription, List<Long> ids,Long doctorId) {
        // 添加病症名称
        pharmacyMapper.additiveDiseaseName(prescription);
        // 添加常用处方中间表
        pharmacyMapper.addCommonPrescriptionIntermediateTable(prescription.getPrescriptionId(),ids,doctorId);
    }
    /**
     * B端 我的药房 常用处方删除
     */
    @Override
    public void commonPrescriptionDeletion(Long prescriptionId) {
        // 通过处方id删除中间表
        pharmacyMapper.deleteTheIntermediateTableByPrescriptionId(prescriptionId);
        // 删除处方
        pharmacyMapper.deletionOfPrescription(prescriptionId);
    }
    /**
     * B端 我的药房 常用处方病症详情
     */
    @Override
    public Prescription commonPrescriptionDetails(Long prescriptionId) {
        return pharmacyMapper.commonPrescriptionDetails(prescriptionId);
    }
    /**
     * B端 我的药房 常用处方药物详情
     */
    @Override
    public List<Restoratives> commonPrescriptionDetailsMedicine(Long prescriptionId) {
        // 通过病症id查询中间表的药品id
        List<PrescriptionMiddleRestoratives> middleRestoratives = pharmacyMapper.theDrugIdInTheMiddleTableIsQueriedByTheDiseaseId(prescriptionId);
        List<Long> longs = new ArrayList<>();
        for (PrescriptionMiddleRestoratives middleRestorative : middleRestoratives) {
            longs.add(middleRestorative.getRestorativesId());
        }
        System.out.println("longs:" + longs);
        // 通过药品id查询
        List<Restoratives> restorativesList = pharmacyMapper.searchByDrugId(longs);
        return restorativesList;
    }
}
